<?php 
//This class repersents the Peremissions of a user in a project
//Provides an interface to the Peremissions table in the DB

 require_once('./inc/database.php');
 require_once('./class/class.user.php');
db_connect();

class permissions
{
  /*********member********/	
	private $permissions_id;
	public $user_id;
	public $project_id;
	public $role_type;
	public $Active;


/*************constrctor*****************/
  public function permissions()
  {
        $this->role_type=null;
		$this->Active=1;
	  
  }
  
  /**************method***********************/
  public function  get_id()
  	{
	  	 return $this->project_id;
	}
	
	public function  insert()
		{
			$stringinsert=sprintf("INSERT INTO `permissions`
			(`user_id`, `project_id`, `role_type`,`Active`) 
			VALUES (
			$this->user_id,$this->project_id,'$this->role_type',$this->Active)");
			$queryinsert=mysql_query($stringinsert) or die(mysql_error().' '.$stringinsert);
			 $this->permissions_id=mysql_insert_id();		
		}
		 public function update()
		{
		   if($this->project_id!=null || $this->project_id!="")
		   	{
			 $stringupdate=sprintf("UPDATE `permissions` SET 
			 `user_id`=$this->user_id,
			 `project_id`=$this->project_id,
			 `role_type`=$this->role_type,
			 `Active`=$this->Active;
			  WHERE  `project_id`=$this->project_id
									  ");
			 $queryupdate=mysql_query($stringupdate) or die(mysql_error().' '.$stringupdate);
			 return true ;	
           	}
			return false;
		}
public function getby_id($permissions_id)
{
   $st=sprintf("SELECT * FROM `permissions` WHERE  `permissions_id`=$permissions_id");
   $query=mysql_query($st)or die (mysql_error().' ' .$st);
   $row=mysql_fetch_assoc($query);
   if(mysql_affected_rows()>0)
   	{
		 $this->permissions_id=$row['permissions_id'];
		 $this->user_id=$row['user_id'];
		 $this->project_id=$row['project_id'];
		 $this->role_type=$row['role_type'];
		 $this->Active=$row['Active'];
		 return true;
	}
	else 
	{
	 return 0;	
	}
}

public static function is_permissions($user_id,$project_id)
{
    $st=sprintf("SELECT * FROM `permissions` WHERE `user_id`=$user_id and `project_id`=$project_id AND Active=1");
   $query=mysql_query($st)or die (mysql_error().' ' .$st);
   $row=mysql_fetch_assoc($query);
   if(mysql_affected_rows()>0)
   	{
		return 1;
	}
	else 	
		{
		 return 0;	
		}
	
}

public static function GetPermissionsOfProject($IdProject)
{
   $st=sprintf("SELECT * FROM `permissions` WHERE `project_id`=$IdProject AND Active=1");
   $query=mysql_query($st)or die (mysql_error().' ' .$st);
   
   if(mysql_affected_rows()>0)
   	{  $arrP=array(); 
	  while($row=mysql_fetch_assoc($query))
	   {
		    $user = new user();
		    $user->getby_id($row['user_id']);
			array_push($arrP,$user);
	   }
		return $arrP;
	}
	else 	
		{
		 return 0;	
		}
	
}
public static function Remove($IdUser,$IdProject)
{
 	 $st=sprintf("DELETE FROM `permissions` WHERE `user_id`=$IdUser AND `project_id`=$IdProject");
   $query=mysql_query($st)or die (mysql_error().' ' .$st);
   
   if(mysql_affected_rows()>0)
   	{ 
	  return true;
	}
	else 	
		{
		 return 0;	
		}
}

  
}
?>